package com.example.exchangeapp.yy.fragment;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.exchangeapp.R;
import com.example.exchangeapp.pxx.CartActivity;
import com.example.exchangeapp.pxx.ModifyActivity;
import com.example.exchangeapp.pxx.RecordsActivity;
import com.example.exchangeapp.pxx.SoldActivity;
import com.example.exchangeapp.zjs.GetUserStatus;
import com.example.exchangeapp.zjs.MySQLiteOpenHelper;
import com.example.exchangeapp.zjs.User;
import com.example.exchangeapp.zjs.login;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends Fragment  {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    //用户头像
    private ImageView avatar;
    //用户电话
    private TextView phone;
    //已购买商品按钮
    private Button cartBtn;
    //交易记录按钮
    private Button recordBtn;
    //已卖出商品按钮
    private Button soldBtn;
    //修改个人信息按钮
    private Button modifyBtn;
    //退出登录按钮
    private Button exitLogin;
    private TextView money;
    private User user;
    //上下文
    private Context mContext;
    private List<User> userList;

    public MyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyFragment newInstance(String param1, String param2) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        GetUserStatus getUserStatus=new GetUserStatus(getActivity());
        Cursor cursor = getUserStatus.getDataFromDatabase();
        userList=new ArrayList<>();

        avatar=getView().findViewById(R.id.avatar);
        phone=getView().findViewById(R.id.phone);
        cartBtn=getView().findViewById(R.id.cartBtn);
        recordBtn=getView().findViewById(R.id.recordBtn);
        soldBtn=getView().findViewById(R.id.soldBtn);
        exitLogin=getView().findViewById(R.id.exit);
        modifyBtn=getView().findViewById(R.id.modifyBtn);
        money=getView().findViewById(R.id.money);

        int idIndex = cursor.getColumnIndex("ID");
        int phoneIndex = cursor.getColumnIndex("phoneNumber");
        int moneyIndex = cursor.getColumnIndex("money");
        int avatarIndex=cursor.getColumnIndex("avatar");

        while (cursor.moveToNext()) {
            User user=new User();

            Integer id = cursor.getInt(idIndex);
            String phone = cursor.getString(phoneIndex);
            Double money=cursor.getDouble(moneyIndex);
            String avatar=cursor.getString(avatarIndex);

            Bitmap bitmap = BitmapFactory.decodeStream(
                    getClass().getResourceAsStream("/" + avatar));

            user.setId(id);
            user.setPhoneNumber(phone);
            user.setMoney(money);
            user.setAvatar(avatar);
            userList.add(user);
        }
        user=userList.get(0);
        phone.setText(user.getPhoneNumber());
        money.setText("账号余额："+user.getMoney()+"元");
        String url=user.getAvatar();
        if(url!=null){
            Glide.with(this).load(url).into(avatar);
        }

        mContext=getActivity();
        cartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, CartActivity.class);
                intent.putExtra("id",user.getId().toString());
                startActivity(intent);
            }
        });

        recordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, RecordsActivity.class);
                intent.putExtra("id",user.getId().toString());
                startActivity(intent);
            }
        });

        soldBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, SoldActivity.class);
                intent.putExtra("id",user.getId().toString());
                startActivity(intent);
            }
        });

        modifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, ModifyActivity.class);
                intent.putExtra("id",user.getId().toString());
                intent.putExtra("phone",user.getPhoneNumber());
                intent.putExtra("avatar",user.getAvatar());
                startActivity(intent);
            }
        });

        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user=userList.get(0);
                MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(getActivity());
                mySQLiteOpenHelper.getWritableDatabase()
                        .delete("Users","ID=?", new String[]{user.getId()+""});
                Intent intent=new Intent(mContext, login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_my, container, false);
        return view;
    }
}